﻿using Libs;
using WriteForFighting.Game.Asset;
using WriteForFighting.Game.Entity.Effect;

namespace WriteForFighting.Game.Entity
{
    class EntityBullet : EntityBase
    {
        public EntityPlayer LanchedPlayer { get; private set; }

        public EntityBullet(double par1X, double par2Y, double par3Angle, EntityPlayer par4LaunchedPlayer)
            : base(par1X, par2Y, par3Angle)
        {
            CollisionCircleRadius = 3;
            IsOverableField = true;

            LanchedPlayer = par4LaunchedPlayer;
        }

        protected override void OnUpdate(int par1GameTickCount, EntityManager par2PlayersManager,
            EntityManager par3BulletsManager, EntityManager par4EffectsManager)
        {
            if (GetIsOverField())
            {
                IsDeath = true;
            }

            foreach (EntityBase e in par2PlayersManager.Entities)
            {
                if ((e != null) &&
                    (((EntityPlayer)e).PlayerType != LanchedPlayer.PlayerType) &&
                    (GetIsHitEntity(e)))
                {
                    e.Health -= 1;
                    par4EffectsManager.SpawnEntity(new EntityEffectExplosion(X, Y, true));
                    IsDeath = true;
                }
            }
        }

        protected override void OnDraw(int par1GameTickCount)
        {
            DX.SetDrawMode(DX.DX_DRAWMODE_BILINEAR);
            DX.DrawRotaGraph2F((float)X, (float)Y, 4.5f, 5, 1.0d, Angle, Drawable.Bullets[LanchedPlayer.PlayerType], DX.TRUE);
            DX.SetDrawMode(DX.DX_DRAWMODE_NEAREST);
        }

        private bool GetIsOverField()
        {
            return ((X + 10) < 0) ||
                    (600 < (X - 10)) ||
                    ((Y + 10) < 0) ||
                    (600 < (Y - 10));
        }
    }
}
